﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DungDungShit
{
    abstract class Sprite
    {
        public static int g_frameCount = 0;
        protected Texture2D defaultTexture;
        protected IntVector2 frameSize;
        protected IntVector2 currentFrame;
        protected IntVector2 sheetSize;
        private int frameCount;
        protected int drawMultiplier = 1;
        protected int drawFrame = 0;
        protected float layerDepth = 1f;

        public int FrameCount { get { return frameCount; } }

        public IntVector2 Velocity = IntVector2.Zero;
        public IntVector2 Position;
        public IntVector2 SubPosition = IntVector2.Zero;

        protected const int framesPerFrame = 3;	// 건드리지마
        public static readonly IntVector2 TileSize = new IntVector2(128, 128);

        public Sprite(Texture2D defaultTexture, IntVector2 position, IntVector2 frameSize, IntVector2 sheetSize, bool randomFrame = true)
        {
            this.defaultTexture = defaultTexture;
            this.Position = position;
            this.frameSize = frameSize;
            this.currentFrame = randomFrame ? new IntVector2(Util.Random.Next(sheetSize.X), Util.Random.Next(sheetSize.Y)) : new IntVector2(0, 0);
            this.sheetSize = sheetSize;

            this.frameCount = Sprite.g_frameCount;
        }

        public virtual void Update(GameTime gameTime)
        {
            frameCount++;

            drawFrame += drawMultiplier;
            if (drawFrame >= framesPerFrame)
            {
                currentFrame.X += drawFrame / framesPerFrame;
                drawFrame %= framesPerFrame;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.Y += currentFrame.X / sheetSize.X;
                    currentFrame.X %= sheetSize.X;
                    if (currentFrame.Y >= sheetSize.Y)
                        currentFrame.Y %= sheetSize.Y;
                }
            }
        }

        protected void DrawTexture(Texture2D texture, SpriteBatch spriteBatch, Vector2 origin)
        {
            if (Position == null) return;

            Color color = Color.White;

            if (this is NonTile)
                spriteBatch.Draw(texture,
                    new Vector2(Position.X * TileSize.X / 2 + SubPosition.X, Position.Y * TileSize.Y / 2 + SubPosition.Y),
                    new Rectangle(currentFrame.X * frameSize.X,
                        currentFrame.Y * frameSize.Y,
                        frameSize.X, frameSize.Y),
                    color, 0, origin,   
                    1f, SpriteEffects.None, layerDepth);
            else 
                spriteBatch.Draw(texture,
                    new Vector2(Position.X * TileSize.X / 2 + SubPosition.X, Position.Y * TileSize.Y / 2 + SubPosition.Y),
                    new Rectangle(currentFrame.X * frameSize.X,
                        currentFrame.Y * frameSize.Y,
                        frameSize.X, frameSize.Y),
                    color, 0, origin,
                    .5f, SpriteEffects.None, layerDepth);
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 origin)
        {
            DrawTexture(defaultTexture, spriteBatch, origin);
        }
    }
}
